package com.yunzai.register.controller;

import com.yunzai.generic.domain.JSONResult;
import com.yunzai.secure.exception.ServiceException;
import com.yunzai.register.domain.Register;
import com.yunzai.register.service.IRegisterService;
import com.yunzai.generic.service.IEmailService;
import com.yunzai.generic.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/register")
public class RegisterController {

    // 验证码字符集（排除易混淆字符）
    private static final String CHAR_SET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    private static final int CODE_LENGTH = 4; // 验证码长度
    private static final String CODE_KEY_PREFIX = "yunzai_api_register_code:";

    private static final int RETRY_NUMBER= 5; // 验证码重试次数
    private static final String CODE_RETRY_NUMBER_KEY_PREFIX = "yunzai_api_register_code_retry_number:";

    private static final String USER_LOCK_KEY_PREFIX = "yunzai_api_register_user_lock:";

    @Autowired
    private IRedisService redisService;

    @Autowired
    private IRegisterService registerService;

    @Autowired
    private IEmailService emailService;

    @GetMapping("/code/{email}")
    public JSONResult code(@PathVariable("email") String email) {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CHAR_SET.length());
            code.append(CHAR_SET.charAt(index));
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String key = CODE_KEY_PREFIX + uuid;
        // 存储到Redis，设置过期时间（例如5分钟）
        redisService.setValue(key, code, 5, TimeUnit.MINUTES);
        emailService.sendVerificationEmail(email, code.toString());
        JSONResult data = new JSONResult();
        data.put("uuid", uuid);
        return JSONResult.success(data);
    }

    @PostMapping
    public JSONResult register(@RequestBody Register register) {
        verifyCode(register.getCode(), register.getUuid(),register.getUsername());
        int i = registerService.register(register);
        return JSONResult.toJSONResult(i);
    }

    private void verifyCode(String code, String uuid, String username) {
        String isLock = redisService.getValue(USER_LOCK_KEY_PREFIX + username);
        if ("true".equals(isLock)) {
            throw new ServiceException("验证码错误次数已达上限！请10分钟后重试！");
        }
        String key = CODE_KEY_PREFIX + uuid;
        String storedCode = redisService.getValue(key);
        Integer retryNumber = redisService.getValue(CODE_RETRY_NUMBER_KEY_PREFIX + username);
        if (storedCode == null) {
            throw new ServiceException("验证码已过期！");
        } else if (!storedCode.equalsIgnoreCase(code)) {
            if (retryNumber != null) {
                if (retryNumber >= RETRY_NUMBER) redisService.setValue(USER_LOCK_KEY_PREFIX + username, "true", 10, TimeUnit.MINUTES);
                redisService.setValue(CODE_RETRY_NUMBER_KEY_PREFIX + username, retryNumber + 1, 1, TimeUnit.MINUTES);
            } else {
                redisService.setValue(CODE_RETRY_NUMBER_KEY_PREFIX + username, 1, 1, TimeUnit.MINUTES);
            }
            redisService.deleteValue(key);
            throw new ServiceException("验证码错误!");
        }
        redisService.deleteValue(key);
    }
}
